package com.qfedu.sem.service.impl;

import java.util.List;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.ExampleMatcher.GenericPropertyMatchers;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.qfedu.sem.dto.BugVersionDto;
import com.qfedu.sem.po.BugVersion;
import com.qfedu.sem.po.Project;
import com.qfedu.sem.repository.BugVersionRepository;
import com.qfedu.sem.service.BugVersionService;

import wo.common.entity.WoPage;
import wo.common.util.WoUtil;

/**
 * PO实体BugVersion对应的Service接口实现类.
 * @author cailei
 */
@Service
@Transactional
public class BugVersionServiceImpl implements BugVersionService {

	/**
	 * 使用日志打印框架.
	 */
	private final static Logger LOG = LogManager.getLogger(BugVersionServiceImpl.class);

	/**
	 * 注入BugVersionRepository.
	 */
	@Resource // @Autowired
	private BugVersionRepository bugVersionRepository;

	/**
	 * DataTable获取版本列表数据.
	 * @param start 当前页开始索引,从0开始
	 * @param length 当前页最大行数
	 * @param searchContent 查询内容
	 * @param dir 排序方式
	 * @return
	 */
	@Override
	public WoPage<BugVersionDto> getPageData(Long start, Long length, String searchContent, String dir) {
		// 从dao获取数据
		Pageable pageInput = PageRequest.of(start.intValue() / length.intValue(), length.intValue()
			 );
		Page<BugVersion> pageData = null;
		if (WoUtil.isEmpty(searchContent)) {
			pageData = bugVersionRepository.findAll(pageInput);
		} else {
			pageData = bugVersionRepository.findAllBynoLike("%" + searchContent + "%", pageInput);
		}
		// 将PO转化为DTO
		WoPage<BugVersionDto> pr = BugVersionDto.getPageData(pageData.getContent(), pageData.getTotalElements());
		return pr;
	}

	/**
	 * 创建版本
	 * @param dto
	 */
	@Override
	public void create(BugVersionDto dto) {
		BugVersion po = dto.createPO();
		bugVersionRepository.save(po);
	}

	/**
	 * 根据id获取版本数据
	 * @param id
	 * @return
	 */
	@Override
	public BugVersionDto getById(String id) {
		BugVersion po = bugVersionRepository.findById(id).get();
		return new BugVersionDto (po);
	}

	/**
	 * 修改版本
	 * @param dto
	 */
	@Override
	public void update(BugVersionDto dto) {
		BugVersion po = bugVersionRepository.findById(dto.getId()).get();
		dto.updatePO(po);
	}

	/**
	 * 根据id数组删除一个或者多个版本
	 * @param id
	 */
	@Override
	public void delete(String[] id) {
		bugVersionRepository.deleteByIdIn(id);
	}

	@Override
	public List<BugVersionDto> getAllByProject(String projectId) {
		List<BugVersion> pos = bugVersionRepository.findAllByProjectIdOrderById(projectId);
		return BugVersionDto.getDtos(pos);
	}

	@Override
	public List<BugVersionDto> getAll() {
		List<BugVersion> pos = bugVersionRepository.findAllByOrderByProjectId();
		return BugVersionDto.getDtos(pos);
	}

	@Override
	public WoPage<BugVersionDto> getPageData(Long start, Long length, String searchContent, String dir,
			String projectId) {
		// 构造查询条件
		ExampleMatcher m = ExampleMatcher.matching();
		// 构造查询对象
		BugVersion qo = new BugVersion();
		// 设置条件：BugVersion.project.id = projectId
		if (!WoUtil.isEmpty(projectId)) {
			m.withMatcher("project.id", GenericPropertyMatchers.exact());
			Project rObj = new Project();
			rObj.setId(projectId);
			qo.setProject(rObj);
		}
		// 设置条件：BugVersion.no contains searchContent
		if (!WoUtil.isEmpty(searchContent)) {
			m.withMatcher("no", GenericPropertyMatchers.contains());
			qo.setNo(searchContent);
		}
		// 构造Example将查询条件组合起来（条件之间是逻辑与的关系）
		Example<BugVersion> ex = Example.of(qo, m); 
		// 构造分页和排序对象
		Pageable pageInput = PageRequest.of(start.intValue() / length.intValue(), length.intValue());
		// 查询最终分页数据
		Page<BugVersion> pageData = bugVersionRepository.findAll(ex, pageInput);
		// 将PO转化为DTO
		WoPage<BugVersionDto> pr = BugVersionDto.getPageData(pageData.getContent(), pageData.getTotalElements());
		return pr;
	}

}
